import echarts from 'echarts';
// 引入柱状图
import 'echarts/lib/chart/bar';
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
import storageApi from '../../../core/storage/storage';

function constructionSafetyManageController($rootScope, $scope, homeStatsService) {
  'ngInject';

  /*eslint-disable */
  this.param = {
    orgType: 3, // 机构类型 1业主 2监理 3项目部
  };

  this.getSecurityManageSituation = () => {
    this.hasData = false;
    homeStatsService
    .getSecurityManageSituation(this.param)
    .then((res) => {
      if (res.success && res.data && res.data.length > 0) {
        this.hasData = true;
        this.initChart(res.data);
      }
    });
  };

  this.initChart = (data) => {
    const orgNames = data.map((item) => {
      return item.orgName;
    });
    const patrolNums = data.map((item) => {
      return item.patrolNum;
    });
    const problemNums = data.map((item) => {
      return item.problemNum;
    });
    const option = {
      color: ['#6dcbe4','#2dc3e8'],
      tooltip : {
      trigger: 'axis',
        axisPointer : { // 坐标轴指示器，坐标轴触发有效
          type : 'shadow', // 默认为直线，可选为：'line' | 'shadow'
        },
      },
      legend: {
          data:['巡查次数','发现问题']
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true,
      },
      xAxis : [
        {
          type : 'category',
          data : orgNames,
          axisTick: {
            alignWithLabel: true
          },
        },
      ],
      yAxis : [
        {
          type : 'value'
        }
      ],
      series : [
        {
          name:'巡查次数',
          type:'bar',
          barWidth: '20%',
          data: patrolNums,
          label: {
            normal: {
              show: true,
              position: 'top'
            }
          },
        },
        {
          name:'发现问题',
          type:'bar',
          barWidth: '20%',
          data: problemNums,
          label: {
            normal: {
              show: true,
              position: 'top'
            }
          },
        }
      ],
    };
    this.myChart = echarts.init(document.getElementById('construction-safety-manage-chart'));
    this.myChart.setOption(option);
  };

  $scope.$watch('$constructionSafetyManageCtrl.hasData', (newVal) => {
    if (!newVal) {
      setTimeout(() => {
        this.myChart.resize({
          width: 'auto',
          height: 'auto',
        });
      }, 500);
    }
  });

  this.getSecurityManageSituation();
}

export default constructionSafetyManageController;
